local UIFivePlayer = UIBase.New(PanelResNames.UIFivePlayer)

local m_rank_list = nil

function UIFivePlayer.Init(tb)
    UIBase.Init(UIFivePlayer)

    logicMgr.BillboardMgr.req_RankServerPassList(tb.type, tb.step)
end

function UIFivePlayer:OnCreate()
    ListenEvent(Config.EventType.Billboard_rank_pass_list, UIFivePlayer.refreshList)
    
    self:RegisterOnClick(self:GetChild("BgClose"), self.onClick_closePanel)

    self.list = self:GetChild("list_")

    -- self.list.itemRenderer = UIFivePlayer.render_rankItem

    self:SetDynamicList(self.list, self.render_rankItem, nil, true)
end

function UIFivePlayer:render_rankItem(index, obj)
    local data = m_rank_list[index]
    local ctr_empty = obj:GetController("empty")
    local ctr_ranking = obj:GetController("ranking")
    local txt_playername = obj:GetChild("PlayerName")
    local text_day = obj:GetChild("text_day")
    local text_time = obj:GetChild("text_time")

    local icon = obj:GetChild("Head"):GetChild("head"):GetChild("HeadIcon")
    local frame = obj:GetChild("Head"):GetChild("frame")

    ctr_ranking.selectedIndex = index - 1
    if data.name then
        ctr_empty.selectedIndex = 0
        txt_playername.text = data.name
        local t = math.ceil(tonumber(tostring(data.time)) / 1000)
        local date = os.date("*t", t)
        text_day.text = string.format("%s/%s/%s", date.year, date.month, date.day)
        text_time.text = string.format("%02d:%02d", date.hour, date.min)
        self:SetPlayerIcon(icon, data.headIcon, frame, data.headFrame)
    else
        ctr_empty.selectedIndex = 1
    end
end

function UIFivePlayer:onClick_closePanel()
    closeUI(PanelNames.UIFivePlayer)
end

function UIFivePlayer.refreshList(list)
    m_rank_list = {}
    for i=1, 3 do
        if list[i] then
            table.insert(m_rank_list, list[i])
        else
            table.insert(m_rank_list, {})
        end
    end
    UIFivePlayer.list.numItems = #m_rank_list
end

function UIFivePlayer:OnDestroy()
    ListenEvent(Config.EventType.Billboard_rank_pass_list, UIFivePlayer.refreshList)
end

return UIFivePlayer